package cn.shengongying.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author : RXK
 * Date : 2020-12-13 10:52
 * Code Less Think More
 * Desc: 加密工具类
 */
public class MD5Utils {

    private static final String ALGORITHM_NAME = "MD5";

    public static String encript(String content) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance(ALGORITHM_NAME);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }

        char[] chars = content.toCharArray();
        byte[] bytes = new byte[chars.length];
        for (int i = 0; i < chars.length; i++) {
            bytes[i] = (byte) chars[i];
        }
        byte[] digest = messageDigest.digest(bytes);
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            int index = b & 0xff;
            if (index < 16) {
                index = 0;
            }
            sb.append(Integer.toHexString(index));
        }
        return sb.toString();
    }

}
